package com.wang.controller;


import com.wang.dao.UserMapper;
import com.wang.entity.User;
import com.wang.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Classname UsersController
 * @Description TODO
 * @Date 2024/5/24 9:36
 * @Created by pc
 */
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisService redisService;

    @GetMapping("/{id}")
    public User findUserById(@PathVariable("id")Integer id){
        //先去redis中查询是否有数据
        User user = (User)redisService.getObj("user" + id);
        if(user == null){
            user = userMapper.findById(id).get();
            redisService.setObj("user"+id,user,60*2*1000);
        }
        return user;
    }
}
